package com.nf.xiaomi.web.controller;

import com.nf.mvc.ViewResult;
import com.nf.mvc.mapping.RequestMapping;
import com.nf.mvc.support.ResponseVO;
import com.nf.xiaomi.entity.CartItemEntity;
import com.nf.xiaomi.service.ShoppingCartService;
import com.nf.xiaomi.service.impl.ShoppingCartServiceImpl;
import com.nf.xiaomi.vo.CartItemVO;

import java.util.List;

import static com.nf.mvc.handler.HandlerHelper.json;

@RequestMapping("/cart")
public class ShoppingCartController {
    ShoppingCartService service=new ShoppingCartServiceImpl();
    @RequestMapping("/add")
    public ViewResult addItem(Long userID,int detailID,String productName){
        int result = service.addItem(userID, detailID, 1,productName);

        ResponseVO<?> responseVO = result < 1 ? ResponseVO.fail() :
                ResponseVO.success("成功加入购物车",result);
        return json(responseVO);
    }

    @RequestMapping("/show")
    public ViewResult showCart(Long userID){
        List<CartItemVO> cart = service.getUserCartItemList(userID);
        return json(ResponseVO.success(cart));
    }

    @RequestMapping("/del")
    public ViewResult delItem(int itemID){
        int result = service.delItem(itemID);
        ResponseVO<?> responseVO = result < 1 ? ResponseVO.fail() :
                ResponseVO.success("操作成功",result);
        return json(responseVO);
    }

    @RequestMapping("/amount")
    public ViewResult setAmount(int cartID,int amount) {
        int result = service.setAmount(cartID, amount);
        ResponseVO<?> responseVO = result < 1 ? ResponseVO.fail() :
                ResponseVO.success("操作成功",result);
        return json(responseVO);
    }

    @RequestMapping("/getCountAndSubtotal")
    public ViewResult getCountAndSubtotal(Long account){
        List<CartItemEntity> cart = service.getCountAndSubtotal(account);
        return json(ResponseVO.success(cart));
    }
}
